﻿// -----------------------------------------------------------------------
// <copyright file="Company.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace SoftServe.HrManager
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using SoftServe.HrManager.Abstractions;
    using SoftServe.HrManager.Models;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Company
    {
        public string Name { get; set; }

        public event EventHandler<ProjectInitiateEventArgs> ProjectInitiate;

        public event EventHandler<ProjectCompleteEventArgs> ProjectComplete;

        public IEnumerable<OrderDetails> ActiveProjects { get { return mActiveProjects; } }

        public Company()
        {
            mTeams.AddRange(mHrDepartment.FormCompanyDeveloperForce<ICSharpDeveloper>(3,5));
        }

        public int MakeSoftwareOrder(Client client, OrderDetails details)
        {
            if (client == null)
                throw new NullReferenceException("client");

            if (details == null)
                throw new NullReferenceException("details");

            var clientIndex = mClients.IndexOf(client);
            if (clientIndex > -1)
                mClients.Add(client);

            ///Generate next project number
            details.Id = Random.Next(0, int.MaxValue);
            mActiveProjects.Add(details);
            
            ///Generate event
            doProjectInitiate(details);

            return details.Id;
        }



        public void FinishProject(int projectId)
        {
            int prjIndex = mActiveProjects.IndexOf(new OrderDetails() { Id = projectId });
            if (prjIndex > -1)
            {
                ///Generate event
                doProjectComplete(projectId);
                mActiveProjects.RemoveAt(prjIndex);
            }
        }

        private void doProjectInitiate(OrderDetails details)
        {
            if (ProjectInitiate != null)
                ProjectInitiate(this, new ProjectInitiateEventArgs()
                {
                    Details = details
                });
        }

        private void doProjectComplete(int projectId) 
        {
            if (ProjectComplete != null)
                ProjectComplete(this, new ProjectCompleteEventArgs() 
                {
                    Date = DateTime.Now,
                    ProjectID = projectId
                });
        }

        HrDepartment mHrDepartment = new HrDepartment();
        private readonly List<Team> mTeams = new List<Team>();
        private readonly List<Client> mClients = new List<Client>();
        private readonly List<OrderDetails> mActiveProjects = new List<OrderDetails>();

        private static readonly Random Random = new Random();
    }
}
